﻿using ads;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CSharp
{
    public partial class Form1 : Form
    {
        private Net net = null;
        public long bytes = 0;

        public Form1()
        {
            InitializeComponent();
            txtIP.Text = getIP();
            Thread thread = new Thread(new ThreadStart(this.run));
            thread.Start();
        }

        public void run()
        {
            net = new Net();
            try
            {
                Configuration conf = new Configuration();
                conf.ip = txtIP.Text;
                conf.port = Int16.Parse(txtPort.Text);
                conf.transport = Configuration.TRANSPORT_TCP;
                conf.mode = Configuration.MODE_RECEIVER;
                net.receiveCallback(new CallBack(this));

                net.initialize(conf);
                net.start();

                net.loop();
            }
            finally
            {
                net.shutdown();
            }
        }

        public String getIP()
        {
            IPHostEntry host;
            string localIP = "?";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    localIP = ip.ToString();
                }
            }
            Console.WriteLine(localIP);
            return localIP;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lblMsg.Text = bytes + " bytes per sec";
            bytes = 0;
        }

        public class CallBack : ReceiveCallback
        {
            private Form1 form = null;
            public CallBack(Form1 form)
            {
                this.form = form;
            }

            public void onReceive(Context context, Msg msg)
            {
                byte[] payload = null;
                payload = msg.packet.data;
                form.bytes += payload.Length;
                if (!(payload[0] == 2 && payload[payload.Length - 1] == 3))
                {
                    Console.WriteLine("Error!");
                }
                else
                {
                    //Console.WriteLine(payload.Length + " bytes");
                }
            }

            public static int ntohl(byte[] buf)
            {
                byte[] buff = new byte[4];
                Array.Clear(buff, 0, buff.Length);
                Array.Copy(buf, 0, buff, 0, buff.Length);
                if (BitConverter.IsLittleEndian) Array.Reverse(buff);
                int ret = (Int32)BitConverter.ToInt32(buff, 0);
                return ret;
            }

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            net.shutdown();
        }
    }
}
